/*
 * PopupTransfersDialog.cpp
 *
 *  Created on: Nov 26, 2012
 *      Author: guillou
 */

#include <QtGui>
#include <PopupOSAL.hpp>
#include <PopupProgressWidget.hpp>
#include <PopupTransfersDialog.hpp>
#include <PopupSettings.hpp>

using namespace Popup;
using namespace PopupUtils;

PopupTransfersDialog::PopupTransfersDialog(QWidget *p_parent)
 : QDialog(p_parent), m_show(false), m_currentOpacity(0.0), m_moving(false)
{
  setupUi(this);
#if QT_VERSION >= 0x040500
  setAttribute(Qt::WA_TranslucentBackground, true);
#endif
  setWindowFlags(Qt::Tool|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
  setWindowOpacity(m_currentOpacity);
  connect(&m_fadeTimer, SIGNAL(timeout()), this, SLOT(slot_fadeInOut()));
}

void PopupTransfersDialog::show()
{
  if (PopupSettings::instance()->isTransfersDialogBoxEnabled())
  {
    QRect _rect = QApplication::desktop()->availableGeometry();
    int _xpos = _rect.width() - width() - 40;
    int _ypos = _rect.height() - 150; // (100~=<dialog height> + 50 margin)
    move(_xpos, _ypos);
    QDialog::show();

    // Wait 1s before we start displaying dialog in case
    // file transfer would be very quick! This will prevent
    // dialog from appearing and disappearing for small transfers.
    m_show = true;
    m_fadeTimer.start(1000);
  }
}

void PopupTransfersDialog::hide()
{
  m_show = false;
  m_fadeTimer.start(10);
}

void PopupTransfersDialog::slot_fadeInOut()
{
  m_fadeTimer.stop();
  if (m_show) {
    if (m_currentOpacity < 1.0) {
      m_currentOpacity += 0.05;
      setWindowOpacity(m_currentOpacity);
      // trace("Setting opacity to %f", m_currentOpacity);
      m_fadeTimer.start(50);
    }
  } else {
    if (m_currentOpacity > 0.0) {
      m_currentOpacity -= 0.05;
      setWindowOpacity(m_currentOpacity);
      // trace("Setting opacity to %f", m_currentOpacity);
      m_fadeTimer.start(50);
    } else {
      hide();
    }
  }
}

void PopupTransfersDialog::slot_updateFileTransferProgress(FileTransfer p_transfer)
{
  m_layout->setAlignment(Qt::AlignVCenter);

  int _progressValue = (int) (100.0 * (float)p_transfer.nbTransferred /
      (float)p_transfer.totalSize);

  // Find the progressbar
  PopupProgressMap::iterator _it = m_progressWidgets.find(p_transfer.attachmentID);

  // Not found! Create it!
  if (_it == m_progressWidgets.end())
  {
    if (!m_show)
    {
      show();
    }
    if (_progressValue < 100) {
      PopupProgressWidget *_progressWidget =
          new PopupProgressWidget(this,
                                  QString::fromUtf8(p_transfer.filepath.c_str()),
                                  _progressValue);

      m_progressWidgets[p_transfer.attachmentID] = _progressWidget;
      m_layout->addWidget(_progressWidget);
    }
  }
  // Existing progress bar found
  else
  {
    PopupProgressWidget *_progressWidget = _it->second;

    // Update it if receive is not complete
    if (_progressValue < 100) {
      _progressWidget->setProgress(_progressValue);
    }
    // Or remove it
    else {
      if (m_show)
      {
        hide();
      }
      m_progressWidgets.erase(_it);
      delete _progressWidget;
    }
  }
}

//==============================================================================
// QT standard callbacks overriding
//==============================================================================

void PopupTransfersDialog::mousePressEvent(QMouseEvent *event)
{
  if (event->button() == Qt::LeftButton) {
    event->accept();
    if (!m_moving) {
      m_moving = true;
      m_offsetInWidget = mapFromGlobal(event->globalPos());
    }
  }
}
//----------------------------------------------------------

void PopupTransfersDialog::mouseReleaseEvent(QMouseEvent *event)
{
  if (event->button() == Qt::LeftButton) {
    event->accept();
    m_moving = false;
  }
}
//----------------------------------------------------------

void PopupTransfersDialog::mouseMoveEvent(QMouseEvent *event)
{
  if (event->buttons() & Qt::LeftButton) {
    move(event->globalPos() - m_offsetInWidget);
    event->accept();
  }
}
